import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import io.jboot.web.controller.annotation.RequestMapping;
import io.jpress.core.menu.annotation.AdminMenu;
import io.jpress.web.base.AdminControllerBase;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

@RequestMapping(value = "/admin/sql", viewPath = "/")
public class sqlex extends AdminControllerBase {

    @AdminMenu(text = "SQL控制台", groupId = "sql")
    public void index(){

        render("views/jpress_addon_sql.html");
    }

    public void ex(){

        String sql = getPara("sql");

        Object execute1 = Db.execute((Connection conn) -> {
            Statement statement = conn.createStatement();
            try {
                statement.execute(sql);
            } catch (SQLException e) {
                conn.close();
                return e.getMessage();
            }
            conn.close();
            return "执行成功";
        });

        renderJson(Ret.ok().set("msg",execute1));
    }
}
